package za.co.dylanjaneke.airdownloader.command
{
	import flash.events.SQLEvent;
	
	import za.co.dylanjaneke.airdownloader.controller.data.ConnectionManager;
	import za.co.dylanjaneke.airdownloader.events.ClearDownloadQueueEvent;
	import za.co.dylanjaneke.airdownloader.events.ShowDownloadQueueEvent;
	import za.co.dylanjaneke.airdownloader.model.vo.FileDownloadVO;

	/**
	 * Author: Dylan Janeke.
	 */
	[Name("clearDownloadQueueCommand")]
	public class ClearDownloadQueueCommand
	{
		[Inject]
		public var repo:ConnectionManager;
		
		[Observer]
		public function handleClearDownloadQueueEvent(event:ClearDownloadQueueEvent): void
		{
			repo.executeStatement(
				FileDownloadVO.createTrashAll(
					repo.createStatement()), onQueueCleared);
		}
		
		protected function onQueueCleared(event:SQLEvent): void
		{
			GlobalEventDispatcher.raiseEvent(new ShowDownloadQueueEvent());
		}
	}
}